package com.mtzqc.common.rv.adapter

import android.content.Context
import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.chad.library.adapter.base.BaseMultiItemAdapter
import com.mtzqc.common.rv.holder.VBViewHolder
import com.mtzqc.common.rv.holder.XViewHolder
import com.scwang.smart.refresh.layout.SmartRefreshLayout

abstract class MultiBindingQuickAdapter<T>() :
    BaseMultiItemAdapter<T>() {


    inline fun <reified H : XViewHolder> addViewItemType(
        type: Int, layoutRes: Int, crossinline holderGet: (View) -> H,
        crossinline onBind: (H, Int, T?) -> Unit
    ) = addItemType(type, object : OnMultiItemAdapterListener<T, H> { // 类型 1
        override fun onBind(holder: H, position: Int, item: T?) {
            onBind.invoke(holder, position, item)
        }

        override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): H {
            return holderGet.invoke(
                LayoutInflater.from(parent.context).inflate(layoutRes, parent, false)
            )
        }
    })

    inline fun <VB : ViewBinding, reified H : VBViewHolder<VB>> addBindItemType(
        type: Int,
        crossinline holderGet: (LayoutInflater, ViewGroup) -> H,
        crossinline onBind: (H, VB, Int, T?) -> Unit
    ) = addItemType(type, object : OnMultiItemAdapterListener<T, H> { // 类型 1
        override fun onBind(holder: H, position: Int, item: T?) {
            onBind.invoke(holder, holder.getViewBinding(), position, item)
        }

        override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): H {
            return holderGet.invoke(
                LayoutInflater.from(parent.context),
                parent
            )
        }

    })
/*
    override fun onCreateDefViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): VBViewHolder<ViewBinding> {
        val generator =
            layouts.get(viewType) ?: throw RuntimeException("ViewType: $viewType not found")
        var sm: SmartRefreshLayout? = null
        sm?.autoLoadMore()
        return VBViewHolder(generator(LayoutInflater.from(parent.context), parent, false))
    }


    */
    /**
     * 调用此方法，设置多布局
     * @param type Int
     *//*
    open fun addItemType(type: Int, binding: (LayoutInflater, ViewGroup, Boolean) -> ViewBinding) {
        layouts.put(type, binding)
    }

    override fun convert(holder: VBViewHolder<ViewBinding>, item: T) {
        onConvert(holder, holder.getViewBinding(), item)
    }

    abstract fun onConvert(holder: VBViewHolder<ViewBinding>, vb: ViewBinding, item: T)*/
}